What is ordered-read-streams?
The ordered-read-streams npm package allows you to combine multiple readable streams into a single readable stream, ensuring that the data from each stream is read in the order the streams were provided. This can be particularly useful when you need to process multiple streams sequentially.
What are ordered-read-streams's main functionalities?
Combining Multiple Streams
This feature allows you to combine multiple readable streams into a single stream. The data from each stream is read in the order the streams were provided. In this example, the contents of 'file1.txt', 'file2.txt', and 'file3.txt' are read sequentially and output to the console.
const OrderedReadStreams = require('ordered-read-streams');
const fs = require('fs');
const stream1 = fs.createReadStream('file1.txt');
const stream2 = fs.createReadStream('file2.txt');
const stream3 = fs.createReadStream('file3.txt');
const combinedStream = new OrderedReadStreams([stream1, stream2, stream3]);
combinedStream.on('data', (chunk) => {
console.log(chunk.toString());
});
combinedStream.on('end', () => {
console.log('All streams have been read in order.');
});
Other packages similar to ordered-read-streams
multistream
The multistream package also allows you to combine multiple streams into a single stream. However, unlike ordered-read-streams, multistream provides more flexibility by allowing you to dynamically add streams during the reading process. This can be useful if the streams to be combined are not known upfront.
merge-stream
The merge-stream package allows you to merge multiple streams into one. Unlike ordered-read-streams, merge-stream does not guarantee the order of the streams. It is useful when you need to process multiple streams concurrently and do not require the data to be in a specific order.
stream-combiner2
The stream-combiner2 package allows you to combine multiple streams into a pipeline. While it does not specifically focus on maintaining the order of streams, it is useful for creating complex stream processing pipelines where the output of one stream is the input to another.
ordered-read-streams
Combines array of streams into one read stream in strict order.
Installation
npm install ordered-read-streams
Overview
ordered-read-streams
handles all data/errors from input streams in parallel, but emits data/errors in strict order in which streams are passed to constructor. This is objectMode = true
stream.
Example
var through = require('through2');
var Ordered = require('ordered-read-streams');
var s1 = through.obj(function (data, enc, next) {
var self = this;
setTimeout(function () {
self.push(data);
next();
}, 200)
});
var s2 = through.obj(function (data, enc, next) {
var self = this;
setTimeout(function () {
self.push(data);
next();
}, 30)
});
var s3 = through.obj(function (data, enc, next) {
var self = this;
setTimeout(function () {
self.push(data);
next();
}, 100)
});
var streams = new Ordered([s1, s2, s3]);
streams.on('data', function (data) {
console.log(data);
})
s1.write('stream 1');
s1.end();
s2.write('stream 2');
s2.end();
s3.write('stream 3');
s3.end();
Ouput will be:
stream 1
stream 2
stream 3
Licence
MIT